package net.goutalk.glcs.module.bi.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.bi.dto.AddVisualMapDto;
import net.goutalk.glcs.module.bi.dto.VisualMapPageDto;
import net.goutalk.glcs.module.bi.entity.VisualMap;
import net.goutalk.glcs.module.bi.service.IVisualMapService;
import net.goutalk.glcs.module.bi.dto.UpdateVisualMapDto;
import net.goutalk.glcs.module.bi.vo.VisualMapPageVo;
import net.goutalk.glcs.module.bi.vo.VisualMapVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(GlobalConstant.BI_MODULE_PREFIX + "/map")
@Api(value = GlobalConstant.BI_MODULE_PREFIX + "/map", tags = "BI地图设计")
@AllArgsConstructor
public class VisualMapController {

    private final IVisualMapService visualMapService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "获取列表数据/(分页)")
    public R list(@Valid @RequestBody VisualMapPageDto dto) {
        LambdaQueryWrapper<VisualMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(VisualMap.class, x -> VoToColumnUtil.fieldsToColumns(VisualMapPageVo.class).contains(x.getProperty()));
        IPage<VisualMap> page = visualMapService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<VisualMapPageVo> pageOutput = ConventPage.getPageOutput(page, VisualMapPageVo.class);
        return R.ok(pageOutput);
    }


    @GetMapping(value = "/info")
    @ApiOperation("根据id查询分类详情")
    public R info(@RequestParam Long id) {
        VisualMap visualMap = visualMapService.getById(id);
        if (visualMap == null) {
            return R.error("查不到此分类信息");
        }
        return R.ok(BeanUtil.toBean(visualMap, VisualMapVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增地图")
    public R add(@Valid @RequestBody AddVisualMapDto dto) {
        long count = visualMapService.count(Wrappers.<VisualMap>query().lambda().eq(VisualMap::getName, dto.getName()));
        if (count > 0) {
            return R.error("地图名称已经存在！");
        }
        VisualMap visualMap = BeanUtil.toBean(dto, VisualMap.class);
        return R.ok(visualMapService.save(visualMap));
    }


    @PutMapping
    @ApiOperation(value = "更新地图")
    public R update(@Valid @RequestBody UpdateVisualMapDto dto) {
        VisualMap visualMap = BeanUtil.toBean(dto, VisualMap.class);
        return R.ok(visualMapService.updateById(visualMap));
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(visualMapService.removeBatchByIds(ids));
    }

}
